'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Imports JCsTools.JCQ.ViewModel

Public Class ContactContextMenuService
    Inherits IcqInterface.ContextService
    Implements IContactContextMenuService

    Public Sub New(ByVal context As IcqInterface.Interfaces.IContext)
        MyBase.New(context)
    End Sub

    Public Function GetContextMenuItems(ByVal contact As ContactViewModel) As IEnumerable(Of MenuItem) Implements IContactContextMenuService.GetMenuItems
        ' Send text-message
        ' Add to contactlist
        ' Remove from contactlist
        ' View history
        ' View info

        Dim item As MenuItem
        Dim items As List(Of MenuItem)

        Dim svStorage As IcqInterface.Interfaces.IStorageService

        svStorage = Context.GetService(Of IcqInterface.Interfaces.IStorageService)()

        items = New List(Of MenuItem)

        item = New MenuItem
        item.Header = "Send text-message"

        AddHandler item.Click, AddressOf OnSendTextmessageClick

        items.Add(item)

        If svStorage.IsContactStored(contact.Model) Then
            item = New MenuItem
            item.Header = "Remove from contactlist"

            AddHandler item.Click, AddressOf OnRemoveContactClick

            items.Add(item)
        Else
            item = New MenuItem
            item.Header = "Add to contactlist"

            AddHandler item.Click, AddressOf OnAddContactClick

            items.Add(item)
        End If

        If ApplicationService.Current.Context.GetService(Of IContactHistoryService).GetHistory(contact, 1).Count > 0 Then
            item = New MenuItem
            item.Header = "View chat history"

            AddHandler item.Click, AddressOf OnViewHistoryClick

            items.Add(item)
        End If

        item = New MenuItem
        item.Header = "View contact details"

        AddHandler item.Click, AddressOf OnViewDetailsClick

        items.Add(item)

        Return items
    End Function

#Region " ContextMenu Item Click Handlers "

    Private Sub OnSendTextMessageClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim fe As FrameworkElement
        Dim contact As ContactViewModel

        Try
            fe = DirectCast(e.OriginalSource, FrameworkElement)
            contact = DirectCast(fe.DataContext, ContactViewModel)

            ApplicationService.Current.Context.GetService(Of IContactWindowViewModelService).GetMessageWindowViewModel(contact).Show()
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Private Sub OnViewHistoryClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim fe As FrameworkElement
        Dim contact As ContactViewModel

        Dim wnd As HistoryWindow

        Try
            fe = DirectCast(e.OriginalSource, FrameworkElement)
            contact = DirectCast(fe.DataContext, ContactViewModel)

            wnd = New HistoryWindow(contact)
            wnd.Show()
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Private Sub OnViewDetailsClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim fe As FrameworkElement
        Dim contact As ContactViewModel

        Dim wnd As ContactInformationWindow

        Try
            fe = DirectCast(e.OriginalSource, FrameworkElement)
            contact = DirectCast(fe.DataContext, ContactViewModel)

            wnd = New ContactInformationWindow(contact)
            wnd.Show()
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Private Sub OnRemoveContactClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim fe As FrameworkElement
        Dim contact As ContactViewModel

        Dim sv As IcqInterface.Interfaces.IStorageService

        Try
            fe = DirectCast(e.OriginalSource, FrameworkElement)
            contact = DirectCast(fe.DataContext, ContactViewModel)

            sv = ApplicationService.Current.Context.GetService(Of IcqInterface.Interfaces.IStorageService)()
            sv.RemoveContact(contact.Model, contact.Group.Model)
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Private Sub OnAddContactClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim fe As FrameworkElement
        Dim contact As ContactViewModel

        Dim svStorage As IcqInterface.Interfaces.IStorageService

        Try
            fe = DirectCast(e.OriginalSource, FrameworkElement)
            contact = DirectCast(fe.DataContext, ContactViewModel)

            svStorage = ApplicationService.Current.Context.GetService(Of IcqInterface.Interfaces.IStorageService)()
            svStorage.AddContact(contact.Model, svStorage.MasterGroup.Groups(0))
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

#End Region

End Class
